package com.ghh.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author hgu
 *
 */
public class MyInterfaceProxy implements InvocationHandler{
	private Object obj;
	
	public MyInterfaceProxy(Object obj) {
		this.obj = obj;
	}
	
	private Object proxy() {
		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("before call method " + method.getName());
		Object result = method.invoke(obj, args);
		System.out.println("after call method " + method.getName());
		return result;
	}

	public static Object createProxy(Object o) {
		return new MyInterfaceProxy(o).proxy();
	}
}
